package com.breeze.queue;

/**
 * 循环队列
 * 顺序结构实现
 */
public class CircularQueue<T> {
    private T[] datas;
    private final int MAX_SIZE = 5;
    private int front, rear;

    public CircularQueue() {
        this.datas = (T[]) new Object[MAX_SIZE + 1];
    }

    public CircularQueue(int size) {
        this.datas = (T[]) new Object[size + 1];
    }

    public void enqueue(T e) {
        if ((rear + 1) % datas.length == front) {
            System.out.println("队列已满");
            return;
        }
        datas[rear] = e;
        rear = (rear + 1) % datas.length;
    }

    public T dequeue() {
        if (front == rear) {
            System.out.println("队列为空");
            return null;
        }
        T res = datas[front];
        front = (front + 1) % datas.length;
        return res;
    }

    public void show() {
        System.out.println("front=" + front + ", rear=" + rear);
        for (int i = 0; i < datas.length; i++) {
            System.out.print(datas[i] + ", ");
        }
    }
}
